﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Text;
public partial class UserControl_ControlChiNhanh : System.Web.UI.UserControl
{
    string str = webapp4U.Configuration.CurrentConnectionString;
    ClassExecute.ClassExecute dt;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                //ViewState["flag"] = false;
                //ClearAdd();
            }
            LoadGrid();
            //nằm trong thread
          
            //ThreadStart thr2 = new ThreadStart(LoadThuocChiNhanh);
          

          
            //Thread tr2 = new Thread(thr2);
           
           
            //tr2.Start();
           

          
            //tr2.Join();
            
            //nằm trong thread
        }
    }

    //void ClearAdd()
    //{
    //    TabContainer1.ActiveTabIndex = 0;
    //    txtMaSo.Text = "New";
    //    txtName.Text = "";
    //    txtDiaChi.Text = "";
    //    txtPhone.Text = "";
    //    ViewState["flag"] = true;

    //    btnSave.Text = "Lưu";
    //    lblMsg.Text = "";
    //    lblMsg.Visible = false;
    //    txtName.Focus();
    //}
   
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    private int RandomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }
    private string RandomString(int size, bool lowerCase)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (lowerCase)
        {
            return builder.ToString().ToLower();
        }
        else
        {
            return builder.ToString();
        }
    }
    public string GetPassword()
    {
        StringBuilder builder = new StringBuilder();
        builder.Append(RandomString(4, true));
        builder.Append(RandomNumber(1000, 9999));
        builder.Append(RandomString(2, false));
        return builder.ToString();
    } 
    
    
  
    protected void grvBranch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvBranch.PageIndex = e.NewPageIndex;
        LoadGrid();
    }
    protected void grvBranch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {
                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblMaSo = (Label)grv.FindControl("lblMaSo");

                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetailBranchList", CommandType.StoredProcedure,
                    new SqlParameter("@Sitecode", lblMaSo.Text));

                txtMaSo.Text = db.Rows[0]["Sitecode"].ToString();
                txtName.Text = db.Rows[0]["sitename"].ToString();
                txtDiaChi.Text = db.Rows[0]["Address"].ToString();
                txtPhone.Text = db.Rows[0]["Tel"].ToString();
                txtFax.Text = db.Rows[0]["Fax"].ToString();
               
                try
                {
                    ddlChiNhanh.SelectedValue = db.Rows[0]["parentSite"].ToString();
                }
                catch
                {

                }
               

                TabContainer1.ActiveTabIndex = 0;
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    //void LoadThuocChiNhanh()
    //{
    //    dt = new ClassExecute.ClassExecute();
    //    try
    //    {

    //        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getBranchListDemand", CommandType.StoredProcedure);
    //        dt.BindDropDownList(db,ddlChiNhanh, "Sitecode", "sitename");
    //    }
    //    catch (Exception ex)
    //    {

    //        string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
    //        dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
    //    }
    //}
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_showbranchlist", CommandType.StoredProcedure);
           
            if (db.Rows.Count == 0)
            {
                grvBranch.DataSource = null;
           
                ShowMessageBox("Không tìm thấy kết quả");
            }
            else
            {
                grvBranch.DataSource = db;
           
            }
            grvBranch.DataBind();
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i = 0;
        try
        {
           
                //update
            i = dt.ExecuteSQl_int(str, "k_updateBranchlist", CommandType.StoredProcedure, ref error,
               new SqlParameter("@Sitecode", txtMaSo.Text),
               new SqlParameter("@sitename", txtName.Text),
               new SqlParameter("@Address", txtDiaChi.Text),
               new SqlParameter("@Tel", txtPhone.Text),
               new SqlParameter("@Fax", txtFax.Text));
                if (i == 1)
                {
                    ShowMessageBox("Cập nhật thông tin thành công");
                    LoadGrid();
                }
                else
                {
                    ShowMessageBox("Hệ thống đang bảo trì");
                   
                }

           
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
  
}